package com.banciyuan.bcywebview.biz.event;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Event;

/**
 * Created by squall on 2015/9/28.
 */
public class EventListAdapter extends BaseAdapter {

    private List<Event> elist;

    private Context mContext;

    private LayoutInflater inflater;

    private ImageLoader imageLoader;

    public EventListAdapter(Context mContext, List<Event> elist) {
        this.mContext = mContext;
        this.elist = elist;
        this.inflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return elist.size();
    }

    @Override
    public Object getItem(int i) {
        return elist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        ImageView img;
        ImageView status;
        TextView tv;

        public ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.event_footer_img);
            status = (ImageView) view.findViewById(R.id.event_status_img);
            tv = (TextView) view.findViewById(R.id.enent_name);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.event_list_title_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        Event event = elist.get(i);

        if (!TextUtils.isEmpty(event.getCover())) {
            final ViewHolder finalViewHolder = viewHolder;
            imageLoader.displayImage(event.getCover(), viewHolder.img, ImageOptions.options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    int width = CommonUtils.getWidthPixels((Activity) mContext) - UIUtils.dip2px(32, mContext);
                    int height = width * loadedImage.getHeight() / loadedImage.getWidth();
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
                    finalViewHolder.img.setLayoutParams(params);
                    finalViewHolder.img.setImageBitmap(loadedImage);
                }
            }, null, true);
        }
        if (!TextUtils.isEmpty(event.getTitle()) && !TextUtils.isEmpty(event.getType_name())) {
            viewHolder.tv.setText(Html.fromHtml(String.format(mContext.getString(R.string.event_name), event.getType_name(), event.getTitle())));
        }
        if (event.isEvent_end()) {
            viewHolder.status.setImageDrawable(mContext.getResources().getDrawable(R.drawable.event_end));
        } else {
            viewHolder.status.setImageDrawable(mContext.getResources().getDrawable(R.drawable.event_ing));
        }

        return view;
    }
}
